package com.tanquandan.askbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanquandan.askbackend.dao.UserMapper;
import com.tanquandan.askbackend.dto.R;
import com.tanquandan.askbackend.entity.User;
import com.tanquandan.askbackend.service.IUserService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public R<User> login(HttpServletRequest request, User user) {
        HttpSession session = request.getSession();
        // 1. 查找username存在的一列
        User findUser = this.query().eq("user_name",user.getUserName()).one();
        // 2. 如果查找到了，则直接返回
        if(findUser != null){
            session.setAttribute("user",findUser.getId());
            return R.success(findUser);
        }
        // 3. 如果没有查找到，则新增，再返回
        User saveUser = new User();
        saveUser.setUserName(user.getUserName());
        this.save(saveUser);
        System.out.println("id:"+saveUser.getId());
        session.setAttribute("user",saveUser.getId());
        return R.success(saveUser);
    }
}
